package com.cloud.easy.im.handler.strategy.websocket;

import com.cloud.easy.im.argeement.Agreement;
import com.cloud.easy.im.argeement.AgreementType;
import com.cloud.easy.im.event.MessageQoSEventListenerS2C;
import com.cloud.easy.im.event.ServerEventListener;
import com.cloud.easy.im.processor.CommonProcessor;
import com.cloud.easy.im.processor.OnlineProcessor;
import com.cloud.easy.im.utils.LocalSendHelper;
import com.cloud.easy.im.handler.strategy.AgreementStrategy;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 处理客户端发挥的ack请求包
 *
 * @author wangjian
 * @version 1.0.0
 * @create 2020年10月12日11:42
 */
public class AgreementWebSocketCRecivedHandler extends AgreementStrategy {

    private static final Logger logger = LoggerFactory.getLogger(AgreementWebSocketCRecivedHandler.class);

    private final MessageQoSEventListenerS2C messageQosEventListener;

    private final CommonProcessor commonProcessor;

    public AgreementWebSocketCRecivedHandler(MessageQoSEventListenerS2C messageQosEventListener) {
        super(AgreementType.C.FROM_CLIENT_TYPE_OF_RECIVED);
        this.messageQosEventListener = messageQosEventListener;
        this.commonProcessor = CommonProcessor.getInstance();
    }

    @Override
    public void handler(Channel session, Agreement agreement, ServerEventListener serverEventListener) throws Exception {
        logger.debug("[WEB_SOCKET_CRECIVED_HANDLER]<< 收到客户端"+agreement.getFrom()+"的ACK应答包发送请求.");

        if(!OnlineProcessor.isOnline(session))
        {
            LocalSendHelper.replyDataForUnlogined(session, agreement, null);
            return;
        }

        commonProcessor.processACK(agreement, messageQosEventListener);
    }
}
